﻿using DGSFramework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;

namespace Framework.Resources
{
    /// <summary>
    /// Summary description for GetStockInfo
    /// </summary>
    public class GetStockInfo : IHttpHandler
    {
        DGSFrameworkEntities entity = new DGSFrameworkEntities();
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "application/json";
            JavaScriptSerializer jsonSerializer = new JavaScriptSerializer();
            try
            {
                var stock = HttpContext.Current.Request.Form["stock"].ToString();
                var date = HttpContext.Current.Request.Form["gettime"].ToString();
                var currentDate = DateTime.Parse(date);
                var stockList = entity.dgs_StockDatas.Where(d => d.dgs_Stocks.Code == stock && d.GetTime <= currentDate).Select(d => new
               {
                   GetTime = d.GetTime,
                   GiaMoCua = d.GiaMoCua,
                   Cao = d.Cao,
                   Thap = d.Thap,
                   KL_Gia = d.KL_Gia,
                   TongKL = d.TongKL
               }).OrderBy(d => d.GetTime).ToList();
                var stockListReturn = stockList.Select(d => new object[]{
                    d.GetTime.Value.ToString("yyyy-MM-dd"),
                    d.GiaMoCua,
                    d.Cao,
                    d.Thap,
                    d.KL_Gia,
                    d.TongKL
                }).ToArray();
                context.Response.Write(
                    jsonSerializer.Serialize(stockListReturn)
                );
            }
            catch (Exception ex)
            {

            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}